package com.example.hospital.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospital.dto.AppointmentQuery;
import com.example.hospital.dto.QueueQuery;
import com.example.hospital.entity.Appointment;
import com.example.hospital.entity.Queue;
import com.example.hospital.service.AppointmentService;
import com.example.hospital.service.QueueService;
import com.example.hospital.mapper.QueueMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author Oreki
 * @description 针对表【queue】的数据库操作Service实现
 * @createDate 2024-12-27 16:53:20
 */
@Service
public class QueueServiceImpl extends ServiceImpl<QueueMapper, Queue>
        implements QueueService {

    @Resource
    AppointmentService appointmentService;

    @Override
    public List<Queue> listQueues(QueueQuery queueQuery) {
        if (queueQuery == null) {
            return list();
        }
        Integer appointmentId = queueQuery.getAppointmentId();
        Integer queueNumber = queueQuery.getQueueNumber();
        String calledStatus = queueQuery.getCalledStatus();
        return lambdaQuery()
                .eq(appointmentId != null, Queue::getAppointmentId, appointmentId)
                .eq(queueNumber != null, Queue::getQueueNumber, queueNumber)
                .eq(StrUtil.isNotEmpty(calledStatus), Queue::getCalledStatus, calledStatus)
                .list();
    }

    @Override
    public List<Queue> listQueuesByPatientId(Integer patientId) {
        AppointmentQuery appointmentQuery = new AppointmentQuery();
        appointmentQuery.setPatientId(patientId);
        List<Appointment> appointments = appointmentService.listAppointments(appointmentQuery);
        if (appointments == null || appointments.isEmpty()) {
            return Collections.emptyList();
        }
        return this.lambdaQuery()
                .in(Queue::getAppointmentId, appointments.stream().map(Appointment::getAppointmentId).toArray())
                .list();
    }
}




